package weaver.interfaces.cyjt.duss.action;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.weaver.general.BaseBean;
import weaver.common.DateUtil;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.interfaces.cyjt.duss.utils.KingdeeUtil;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

/**
 * @Title: ecology-idea
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2024-12-06
 * @Description: OA单据：09.付款结算单
 * @ModifyLog:
 **/
public class WeaFkjsd09Action extends BaseBean implements Action {
    @Override
    public String execute(RequestInfo requestInfo) {
        String requestid = requestInfo.getRequestid();
        String tablename = requestInfo.getRequestManager().getBillTableName();
        writeLog(">>>>>WeaFkjsd09Action>>>>>requestid=" + requestid);
        try {
            String message = "金蝶接口返回：";
            RecordSet rs = new RecordSet();
            KingdeeUtil kingdeeUtil = new KingdeeUtil();
            rs.executeQuery("select * from " + tablename + " where requestid=?", requestid);
            rs.next();
            String mainid = Util.null2String(rs.getString("id"));
            //业务日期
            String djrq = Util.null2String(rs.getString("djrq"));
            //结算方式.编码
            String jsfsbm = Util.null2String(rs.getString("jsfsbm"));
            //付款类型
            String fklxbm = Util.null2String(rs.getString("fklxbm"));
            //付款币种.货币代码
            String bzbm = Util.null2String(rs.getString("bzbm"));
            //付款人（公司）.id
            String org_id = Util.null2String(rs.getString("orgid"));
            //银行账号.id
            String fkyhzhbm = Util.null2String(rs.getString("fkyhzhbm"));
            //收款人类型
            String skrlxbm = Util.null2String(rs.getString("skrlxbm"));
            //收款人类型-客户
            String khbm = Util.null2String(rs.getString("khbm"));
            //收款人类型-供应商
            String gysbm = Util.null2String(rs.getString("gysbm"));
            //收款账号
            String skyhzhbm = Util.null2String(rs.getString("skyhzhbm"));
            //收款银行名称
            String skyhmcdhwb = Util.null2String(rs.getString("skyhmcdhwb"));
            //付款金额折本位币
            String bcfkjehj = Util.null2String(rs.getString("bcfkjehj"));
            //成本中心.编码
            String sqbmjdbm = Util.null2String(rs.getString("sqbmjdbm"));
            JSONObject paramsJson = new JSONObject();
            JSONArray dataList = new JSONArray();
            JSONObject dataJson = new JSONObject();
            dataJson.put("billtype_number", "cas_paybill_other_BT_S");
            dataJson.put("bizdate", DateUtil.getCurrentDate());
            dataJson.put("settletype_number", jsfsbm);
            dataJson.put("paymenttype_number", fklxbm);
            dataJson.put("currency_number", bzbm);
            dataJson.put("org_id", org_id);
            dataJson.put("payeracctbank_id", fkyhzhbm);
            dataJson.put("payeetype", skrlxbm);
            String payee = "";
            if ("bd_customer".equals(skrlxbm)) {
                payee = khbm;
            }
            if ("bd_supplier".equals(skrlxbm)) {
                payee = gysbm;
            }
            dataJson.put("payee", payee);
            dataJson.put("payeebanknum", skyhzhbm);
            dataJson.put("payeebankname", skyhmcdhwb);
            dataJson.put("localamt", bcfkjehj);
            JSONArray entryList = new JSONArray();
            rs.executeQuery("select * from " + tablename + "_dt1 where mainid=?", mainid);
            while (rs.next()) {
                JSONObject entryJson = new JSONObject();
                String id = Util.null2String(rs.getString("id"));
                String fyxmbm = Util.null2String(rs.getString("fyxmbm"));
                String FYSM = Util.null2String(rs.getString("FYSM"));
                String bcfkje = Util.null2String(rs.getString("bcfkje"));
                entryJson.put("e_expenseitem_number", fyxmbm);
                entryJson.put("e_remark", FYSM);
                entryJson.put("e_payableamt", bcfkje);
                entryJson.put("costcenter_number", sqbmjdbm);
                entryList.add(entryJson);
            }
            dataJson.put("entry", entryList);
            dataList.add(dataJson);
            paramsJson.put("data", dataList);
            writeLog(">>>>WeaFkjsd09Action>>>>paramsJson=" + paramsJson);
            JSONObject resultJson = kingdeeUtil.handleKingdeePayBill(paramsJson, requestid);
            writeLog(">>>>WeaFkjsd09Action>>>>resultJson=" + resultJson);
            String retrunmessage = Util.null2String(resultJson.getString("message"));
            if ("success".equals(retrunmessage)) {
                String datas = Util.null2String(resultJson.getString("datas"));
                JSONObject returnDatas = JSONObject.parseObject(datas);
                String errorCode = returnDatas.getString("errorCode");
                if ("0".equals(errorCode)) {
                    return Action.SUCCESS;
                } else {
                    message += returnDatas.getString("message");
                }
            } else {
                message += retrunmessage;
            }
            requestInfo.getRequestManager().setMessageid("111111");
            requestInfo.getRequestManager().setMessagecontent(message);
            return Action.FAILURE_AND_CONTINUE;
        } catch (Exception e) {
            writeLog(">>>>>WeaFkjsd09Action>>>>>Exception=" + e);
            requestInfo.getRequestManager().setMessageid("111111");
            requestInfo.getRequestManager().setMessagecontent("Action接口处理异常,请联系管理员");
            return Action.FAILURE_AND_CONTINUE;
        }
    }


}